package com.sky.myapplication.kotlin.kotlinAdapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.sky.myapplication.R
import com.sky.myapplication.java.data.FileEntity

/**
 * @Author: Minxp
 * @UpdateDate: 2021/7/5 0005
 * @Description: recycleView的Adapter
 */
class FileAdapter(val mContext : Context, val list : ArrayList<FileEntity>) : RecyclerView.Adapter<FileAdapter.FileAdapterViewHole>() {
    private lateinit var click : FileAdapterClick

    fun setFileAdapterClick(click: FileAdapterClick){
        this.click = click
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FileAdapterViewHole {
        var view = LayoutInflater.from(mContext).inflate(R.layout.item_file, parent, false)
        return FileAdapterViewHole(view)
    }

    override fun onBindViewHolder(holder: FileAdapterViewHole, position: Int) {
        var entity = list.get(position)

        holder.name.text = list[position].name
        holder.type.visibility = if (list[position].directory) View.VISIBLE else View.GONE

        holder.clItem.setOnClickListener(object : View.OnClickListener{
            override fun onClick(p0: View?) {
                if (click != null){
                    click.itemClick(entity)
                }
            }
        })
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class FileAdapterViewHole(itemView : View) : RecyclerView.ViewHolder(itemView){
        var name = itemView.findViewById(R.id.tv_name) as TextView
        var type = itemView.findViewById(R.id.iv_file) as ImageView
        var clItem = itemView.findViewById(R.id.cl_item) as ConstraintLayout
    }

    interface FileAdapterClick{
        fun itemClick(entity : FileEntity)
    }
}